package com.example.springboot.admin.controller;

import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemcacheController {

    @Autowired
    private MemcachedClient memcachedClient;

    @GetMapping("/memcache")
    public String memcache() throws InterruptedException {
        // 放入缓存,如下key为name,值为liubao,过期时间为5000,单位为毫秒
        OperationFuture<Boolean> flag = memcachedClient.set("name", 5000, "liubao2020");
        // 取出缓存
        Object value = memcachedClient.get("name");
        System.out.println(value);
        // 多线程睡眠5秒
        Thread.sleep(5000);
        value = memcachedClient.get("name");
        System.out.println(value);
        return "sucess";
    }
}
